package com.zhj.leetcode;

/**
 * 力扣344 翻转字符串
 */
public class Test344 {

    public static void main(String[] args) {
        String str = "abcdefg";
        String s = reverseString(str);
        System.out.println(s);
    }

    private static String reverseString(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        char[] chars = s.toCharArray();
        int l = 0;
        int r = chars.length - 1;
        while (l <= r) {
            char temp = chars[l];
            chars[l] = chars[r];
            chars[r] = temp;
            l++;
            r--;
        }
        return chars.toString();
    }

    private static String reverseStringDG(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        char[] chars = s.toCharArray();
        recursion(chars, 0, chars.length - 1);
        return chars.toString();
    }

    private static void recursion(char[] chars, int left, int right) {
        if (left >= right) {
            return;
        }
        recursion(chars, left+1, right-1);
        Character temp = chars[left];
        chars[left] = chars[right];
        chars[right] = temp;
    }
}
